<script setup lang="ts">
import MarkdownIt from 'markdown-it';

definePageMeta({ layout: false });

const route = useRoute();
const token = computed(() => route.params.token as string);

const article = ref<any>(null);
const loading = ref(true);
const error = ref('');

const md = new MarkdownIt({ html: false, linkify: true, breaks: true });
const renderedContent = computed(() => {
  if (!article.value?.content) return '';
  return md.render(article.value.content);
});

async function fetchArticle() {
  loading.value = true;
  try {
    const res = await $fetch<any>(`/api/share/${token.value}`);
    if (res.code === 0) article.value = res.data;
    else error.value = res.message || '文章不存在或链接已失效';
  } catch {
    error.value = '文章不存在或链接已失效';
  }
  loading.value = false;
}

function formatDate(ts: number) {
  return new Date(ts).toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit' });
}

onMounted(fetchArticle);
</script>

<template>
  <div class="share-page">
    <div class="share-container">
      <!-- Header -->
      <div class="share-header">
        <div class="share-logo">万物收藏</div>
        <div class="share-subtitle">分享的文章</div>
      </div>

      <div v-if="loading" class="share-loading">加载中...</div>

      <div v-else-if="error" class="share-error">
        <div class="error-icon">🔗</div>
        <div>{{ error }}</div>
      </div>

      <article v-else class="share-article">
        <h1 class="article-title">{{ article.title }}</h1>
        <div class="article-meta">
          <span>{{ formatDate(article.updatedAt) }}</span>
          <span v-if="article.wordCount">{{ article.wordCount }} 字</span>
        </div>

        <div class="article-content" v-html="renderedContent" />

        <!-- Linked Items -->
        <div v-if="article.linkedItems?.length > 0" class="linked-section">
          <h2>引用收藏</h2>
          <div class="linked-grid">
            <a
              v-for="li in article.linkedItems"
              :key="li.items?.id"
              :href="li.items?.url || '#'"
              target="_blank"
              class="linked-card"
            >
              <div class="linked-type" :class="li.items?.type">{{ li.items?.type }}</div>
              <div class="linked-title">{{ li.items?.title }}</div>
              <div class="linked-desc">{{ li.items?.description }}</div>
            </a>
          </div>
        </div>
      </article>

      <div class="share-footer">
        由 <span class="footer-brand">万物收藏</span> 驱动
      </div>
    </div>
  </div>
</template>

<style>
/* Global reset for share page (no layout) */
body { margin: 0; }
</style>

<style scoped>
.share-page {
  min-height: 100vh;
  background: var(--bg, #0e0f11);
  color: var(--text, #e8e6e1);
  font-family: var(--font-body, 'Inter', sans-serif);
  font-size: 15px;
  display: flex;
  justify-content: center;
}
.share-container {
  max-width: 720px;
  width: 100%;
  padding: 40px 24px 60px;
}
.share-header {
  text-align: center;
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border, rgba(255,255,255,0.08));
}
.share-logo {
  font-family: var(--font-display, 'Georgia', serif);
  font-size: 18px;
  color: var(--accent, #c8a97e);
  letter-spacing: 0.04em;
}
.share-subtitle {
  font-size: 12px;
  color: var(--text3, #6c6a64);
  margin-top: 4px;
}
.share-loading, .share-error {
  text-align: center;
  padding: 60px 0;
  color: var(--text3, #6c6a64);
}
.error-icon { font-size: 40px; margin-bottom: 12px; opacity: 0.5; }
.article-title {
  font-family: var(--font-display, 'Georgia', serif);
  font-size: 28px;
  font-weight: 500;
  line-height: 1.3;
  margin: 0 0 8px;
}
.article-meta {
  font-size: 13px;
  color: var(--text3, #6c6a64);
  display: flex;
  gap: 16px;
  margin-bottom: 32px;
}
.article-content {
  line-height: 1.8;
}
.article-content :deep(h1) { font-size: 22px; font-weight: 500; margin: 24px 0 12px; }
.article-content :deep(h2) { font-size: 19px; font-weight: 500; margin: 22px 0 10px; }
.article-content :deep(h3) { font-size: 16px; font-weight: 500; margin: 18px 0 8px; }
.article-content :deep(p) { margin: 0 0 12px; }
.article-content :deep(code) {
  background: rgba(255,255,255,0.06);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: var(--font-mono, 'DM Mono', monospace);
  font-size: 13px;
}
.article-content :deep(pre) {
  background: var(--bg2, #141518);
  border: 1px solid var(--border, rgba(255,255,255,0.08));
  border-radius: 8px;
  padding: 14px;
  overflow-x: auto;
}
.article-content :deep(pre code) { background: none; padding: 0; }
.article-content :deep(blockquote) {
  border-left: 3px solid var(--accent, #c8a97e);
  padding: 4px 0 4px 14px;
  margin: 10px 0;
  color: var(--text2, #8a8680);
  font-style: italic;
}
.article-content :deep(ul), .article-content :deep(ol) { padding-left: 20px; margin: 8px 0; }
.article-content :deep(li) { margin: 4px 0; }
.article-content :deep(a) { color: var(--accent, #c8a97e); }
.article-content :deep(img) { max-width: 100%; border-radius: 8px; }

.linked-section { margin-top: 40px; padding-top: 24px; border-top: 1px solid var(--border, rgba(255,255,255,0.08)); }
.linked-section h2 {
  font-family: var(--font-display, 'Georgia', serif);
  font-size: 18px;
  font-weight: 500;
  margin: 0 0 12px;
}
.linked-grid { display: flex; flex-direction: column; gap: 8px; }
.linked-card {
  display: block;
  background: var(--bg2, #141518);
  border: 1px solid var(--border, rgba(255,255,255,0.08));
  border-radius: 8px;
  padding: 12px 14px;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.15s;
}
.linked-card:hover { border-color: var(--border2, rgba(255,255,255,0.12)); }
.linked-type {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 10px;
  text-transform: uppercase;
  font-weight: 500;
  margin-bottom: 6px;
}
.linked-type.web { background: rgba(42,130,218,0.15); color: var(--blue, #4a8ed4); }
.linked-type.text { background: rgba(168,124,231,0.15); color: var(--purple, #a87ce7); }
.linked-type.image { background: rgba(95,178,159,0.15); color: var(--teal, #5fb29f); }
.linked-type.video { background: rgba(224,108,117,0.15); color: var(--red, #e06c75); }
.linked-type.file { background: rgba(200,169,126,0.15); color: var(--accent, #c8a97e); }
.linked-title { font-size: 14px; font-weight: 500; margin-bottom: 4px; }
.linked-desc { font-size: 12px; color: var(--text3, #6c6a64); }

.share-footer {
  text-align: center;
  margin-top: 48px;
  padding-top: 20px;
  border-top: 1px solid var(--border, rgba(255,255,255,0.08));
  font-size: 12px;
  color: var(--text3, #6c6a64);
}
.footer-brand { color: var(--accent, #c8a97e); }
</style>
